package com.woniu.inlet.web;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.woniu.command.Command;
import com.woniu.command.warehouseCommand.AddWarehouseCommand;
import com.woniu.command.warehouseCommand.DeleteWarehouseCommand;
import com.woniu.command.warehouseCommand.UpdateWarehouseCommand;
import com.woniu.inlet.vo.WarehouseVo;
import com.woniu.outlet.po.WarehousePo;
import com.woniu.service.WarehouseService;
import com.woniu.utils.ResponseResult;
import com.woniu.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "仓库管理")
@Slf4j
@RequestMapping("/warehouse/")
public class WarehouseController {

    @Autowired
    private WarehouseService warehouseService;

    @Autowired
    private SystemLogController systemLogController;

    @GetMapping("/warehouses")
    @ApiOperation("查询仓库")
    public ResponseResult<List<WarehouseVo>> findWarehouses() throws Exception{
        List<WarehousePo>  warehousePos =  warehouseService.findWarehouses();
        List<WarehouseVo> warehouseVos = new ArrayList<>();
        //对象的深拷贝
        for (WarehousePo item : warehousePos){
            WarehouseVo warehouseVo = new WarehouseVo();
            BeanUtils.copyProperties(item,warehouseVo);
            warehouseVos.add(warehouseVo);
        }
        systemLogController.addSystemLog("查询仓库");
        return new ResponseResult<>(warehouseVos, "查询成功", ResultCode.SUCCESS.getCode());
    }

    @GetMapping("/warehouses/id")
    @ApiOperation("根据id查询仓库")
    public ResponseResult<WarehouseVo> getWarehouseById(Long id) throws Exception {
        WarehousePo warehousePo = warehouseService.getWarehouseById(id);

        //对象的深拷贝

        WarehouseVo warehouseVo = new WarehouseVo();
        BeanUtils.copyProperties(warehousePo, warehouseVo);

        systemLogController.addSystemLog("根据id查询仓库");
        return new ResponseResult<>(warehouseVo,"查询成功", ResultCode.SUCCESS.getCode());
    }


    @PostMapping("/add")
    @ApiOperation("添加仓库")
    @ApiOperationSupport(ignoreParameters = {"id","warehouseCode","spare1","spare2","spare3"})
    public ResponseResult<String> addWarehouse(WarehousePo warehousePo){
        Command command = new AddWarehouseCommand();
        command.execute(warehousePo);

        systemLogController.addSystemLog("添加仓库");
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"添加成功",ResultCode.SUCCESS.getCode());
    }

    @PostMapping("/update")
    @ApiOperation("修改仓库")
    @ApiOperationSupport(ignoreParameters = {"spare1","spare2","spare3"})
    public ResponseResult<String> updateWarehouse(WarehousePo warehousePo){
        Command command = new UpdateWarehouseCommand();
        command.execute(warehousePo);

        systemLogController.addSystemLog("修改仓库");
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"修改成功",ResultCode.SUCCESS.getCode());
    }


    @PostMapping("/delete")
    @ApiOperation("根据id删除仓库")
    public ResponseResult<String> deleteWarehouse(Long id){
        Command command = new DeleteWarehouseCommand();
        command.execute(id);
        systemLogController.addSystemLog("根据id删除仓库");
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"删除成功",ResultCode.SUCCESS.getCode());
    }


}
